package engine.time;

import engine.Globals;

/**
 * Timer Used for keeping track of elapsed time between frames.
 **/

public class Timer {
	public Timer() {
		_LastTime = 0;
		_Elapsed = 0;
	}
	/**
	 * Updates all timer fields.
	 * 
	 * @returns the current system time in Milliseconds.
	 **/
	public long update() {
		// lazy init
		if (!_Initialized) {
			_LastTime = Globals.Platform.getTime();
			_CurrentTime = _LastTime;
			_Elapsed = 0;
			_Initialized = true;
			return _CurrentTime;
		}

		long time = Globals.Platform.getTime();
		if (time != _LastTime) {
			_LastTime = _CurrentTime;
			_CurrentTime = time;
			_Elapsed = (int) (time - _LastTime);
		}
		return time;
	}

	/**
	 * @returns Time elapsed in milliseconds between the last two calls to
	 *          update.
	 **/
	public int getElapsed() {
		return _Elapsed;
	}
	/**
	 * @returns The time it was when update was last called.
	 **/
	public long getCurrentTime() {
		return _CurrentTime;
	}

	private boolean _Initialized;
	private long _LastTime;
	private long _CurrentTime;
	private int _Elapsed;
}